package com.ruoyi.system.carsale.service.impl;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.carsale.domain.CarFct;
import com.ruoyi.system.carsale.mapper.CarSeriesMapper;
import com.ruoyi.system.carsale.domain.CarSeries;
import com.ruoyi.system.carsale.service.ICarFctService;
import com.ruoyi.system.carsale.service.ICarSeriesService;

/**
 * 车系名称Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-18
 */
@Service
public class CarSeriesServiceImpl extends ServiceImpl<CarSeriesMapper, CarSeries> implements ICarSeriesService
{
    @Autowired
    private CarSeriesMapper carSeriesMapper;
    @Autowired
    private ICarFctService carFctService;
    /**
     * 查询车系名称
     * 
     * @param id 车系名称主键
     * @return 车系名称
     */
    @Override
    public CarSeries selectCarSeriesById(Long id)
    {
        return carSeriesMapper.selectCarSeriesById(id);
    }

    /**
     * 查询车系名称列表
     * 
     * @param carSeries 车系名称
     * @return 车系名称
     */
    @Override
    public List<CarSeries> selectCarSeriesList(CarSeries carSeries)
    {
        List<CarSeries> list = carSeriesMapper.selectCarSeriesList(carSeries);
        List<Long> fctIds = list.stream().map(a -> a.getFctId()).collect(Collectors.toList());
        if(fctIds.size() > 0) {
            CarFct fct = new CarFct();
            fct.setFctIds(fctIds.stream().distinct().collect(Collectors.toList()));
            List<CarFct> fctList = carFctService.selectCarFctList(fct);
            Map<Long, CarFct> fctMap = fctList.stream()
                    .collect(Collectors.toMap(CarFct::getFctId, Function.identity(), (k1, k2) -> k1));
            for(CarSeries c : list) {
                CarFct fct1 =  fctMap.get(c.getFctId());
                if(fct1 == null) {
                    continue;
                }
                c.setBrandName(fct1.getBrandName());
                c.setFctName(fct1.getFctname());
            }
        }
        return list;
    }

    /**
     * 新增车系名称
     * 
     * @param carSeries 车系名称
     * @return 结果
     */
    @Override
    public int insertCarSeries(CarSeries carSeries)
    {
        return carSeriesMapper.insertCarSeries(carSeries);
    }

    /**
     * 修改车系名称
     * 
     * @param carSeries 车系名称
     * @return 结果
     */
    @Override
    public int updateCarSeries(CarSeries carSeries)
    {
        return carSeriesMapper.updateCarSeries(carSeries);
    }

    /**
     * 批量删除车系名称
     * 
     * @param ids 需要删除的车系名称主键
     * @return 结果
     */
    @Override
    public int deleteCarSeriesByIds(Long[] ids)
    {
        return carSeriesMapper.deleteCarSeriesByIds(ids);
    }

    /**
     * 删除车系名称信息
     * 
     * @param id 车系名称主键
     * @return 结果
     */
    @Override
    public int deleteCarSeriesById(Long id)
    {
        return carSeriesMapper.deleteCarSeriesById(id);
    }

    @Override
    public Long getMaxSeriesId() {
        return carSeriesMapper.getMaxSeriesId();
    }
}
